//Main code: sang
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "widget-container.h"
#include "login-widget.h"
#include "input-field.h"
#include "string-util.h"
#include "service.h"
#include "main-program.h"

void login_widget_destroy(login_widget_t* login_widget){
	//widget_container_destroy(login_widget);
//	input_field_destroy(login_widget->username_input);
//	input_field_destroy(login_widget->password_input);
//	free(login_widget);
	gtk_main_quit();
}

#define MAX_USERNAME_LENGTH 100
#define STR_MAX_LENGTH 100
#define MAX_PASSWORD_LENGTH 100
void on_login_button_clicked(login_widget_t* data){
	char* username = strndup(input_field_get_value(data->username_input), MAX_USERNAME_LENGTH);
	char* password = strndup(input_field_get_value(data->password_input), MAX_PASSWORD_LENGTH);
	char message[STR_MAX_LENGTH];
	if (is_good_login(username, password, message, STR_MAX_LENGTH)){
		gtk_widget_hide(widget_container_get_controls(*data));
		gtk_widget_show(widget_container_get_controls(*main_program_new_with_login(data)));
	}
	else{
		GtkWidget* dialog= gtk_message_dialog_new(GTK_WINDOW(widget_container_get_controls(*data)),
				GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				GTK_MESSAGE_ERROR,
				GTK_BUTTONS_CLOSE,
				"%s%s", "Login failed\nMessage: ", message);
		gtk_window_set_title(GTK_WINDOW(dialog), "Login failed");
		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
	}
}

login_widget_t* login_widget_new(){
	login_widget_t* ret = malloc(sizeof(login_widget_t));
	//win chinh
	GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	ret->controls = win;
	gtk_window_set_title(GTK_WINDOW(win), "Login");
	gtk_window_set_resizable(GTK_WINDOW(win), FALSE);
	gtk_container_set_border_width(GTK_CONTAINER(win), 5);
	GtkWidget* vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
	
	//thoat khi dong cua so
	g_signal_connect_swapped(win, "destroy", G_CALLBACK(login_widget_destroy), (gpointer)ret);

	//username
	input_field_t* username_input = input_field_new_with_tooltip("_Username", "postgres", "Username to login to postgresql server");
	widget_container_show(*username_input);
	gtk_box_pack_start(GTK_BOX(vbox),widget_container_get_controls(*username_input), FALSE, FALSE, 0);

	//password
	input_field_t*	password_input = input_field_new_with_tooltip("Pass_word", "sang", "Password to login to postgresql server");
	input_field_set_password_field(password_input, TRUE);
	widget_container_show(*password_input);
	gtk_box_pack_start(GTK_BOX(vbox),widget_container_get_controls(*password_input), FALSE, FALSE, 0);

	//remember user name
	GtkWidget* remember_username = gtk_check_button_new_with_mnemonic("_Remember username");
	gtk_widget_set_tooltip_text(remember_username, "Auto fill username for future login");
	gtk_widget_show(remember_username);
	gtk_box_pack_start(GTK_BOX(vbox), remember_username, FALSE, FALSE, 0);
	
	//remember password
	GtkWidget* remember_password = gtk_check_button_new_with_mnemonic("R_emember password");
	gtk_widget_set_tooltip_text(remember_password, "Auto fill password for future login");
	gtk_widget_show(remember_password);
	gtk_box_pack_start(GTK_BOX(vbox), remember_password, FALSE, FALSE, 0);

	//ok-cancel button
	GtkWidget* ok_cancel = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
	gtk_widget_show(ok_cancel);
	//ok button
	GtkWidget* login_button = gtk_button_new_from_stock(GTK_STOCK_OK);
	g_signal_connect_swapped(G_OBJECT(login_button), "clicked", G_CALLBACK(on_login_button_clicked), ret);
	gtk_button_set_label(GTK_BUTTON(login_button), "_Login");
	gtk_widget_set_tooltip_text(login_button, "Try to login");
	gtk_button_set_image(GTK_BUTTON(login_button), GTK_WIDGET(
				gtk_button_get_image(GTK_BUTTON(gtk_button_new_from_stock(GTK_STOCK_OK)))));
	gtk_widget_show(login_button);
	gtk_box_pack_start(GTK_BOX(ok_cancel), login_button, TRUE, FALSE, 0);
	gtk_widget_set_can_default(login_button, TRUE);
	gtk_widget_grab_focus(login_button);
	gtk_window_set_default(GTK_WINDOW(win), login_button);
	//enter action for 2 entries
	
	//cancel button
	GtkWidget* cancel_button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);//"_Cancel");
	g_signal_connect_swapped(cancel_button, "clicked", G_CALLBACK(gtk_widget_destroy), widget_container_get_controls(*ret));
	gtk_widget_show(cancel_button);
	gtk_widget_set_tooltip_text(cancel_button, "Exit main program");
	gtk_box_pack_end(GTK_BOX(ok_cancel), cancel_button, TRUE, FALSE, 0);
	//add to main box
	gtk_box_pack_start(GTK_BOX(vbox), ok_cancel, TRUE, TRUE, 0);

	gtk_container_add(GTK_CONTAINER(win), vbox);
	gtk_widget_show(vbox);

	ret->username_input = username_input;
	ret->password_input = password_input;
	ret->remember_password = remember_password;
	ret->remember_username = remember_username;
	ret->login_button = login_button;
	ret->cancel_button = cancel_button;
	return ret;
}
